﻿package com.cn.mobileproject.db;

import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.cn.mobileproject.db.table.DictionaryTable;
import com.cn.mobileproject.db.table.EnterpriseTable;
import com.cn.mobileproject.db.table.OrgRelationTable;
import com.cn.mobileproject.db.table.OrgTable;
import com.cn.mobileproject.db.table.UserInfoTable;
import com.cn.mobileproject.db.table.UserTable;

public class UDatabase {

    private static final String DATABASE_NAME = "mobileproject.db";
    private static final int DATABASE_VERSION =5;    
    
    private static UDatabase instance = null;
    private static DatabaseHelper mOpenHelper = null;
    private Context mContext = null;

    /**
     * SQLiteOpenHelper
     */
    private static class DatabaseHelper extends SQLiteOpenHelper {
        public DatabaseHelper(Context context, String name,CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        public DatabaseHelper(Context context) {
            this(context, DATABASE_NAME, DATABASE_VERSION);
        }

        public DatabaseHelper(Context context, String name, int version) {
            this(context, name, null, version);
        }

        @Override
        /**
         * 创建数据库信息
         */
        public void onCreate(SQLiteDatabase db) {
        	 db.execSQL(UserTable.CREATE_TABLE);
        	 db.execSQL(DictionaryTable.CREATE_TABLE);
        	 db.execSQL(OrgTable.CREATE_TABLE);
        	 db.execSQL(OrgRelationTable.CREATE_TABLE);
        	 db.execSQL(UserInfoTable.CREATE_TABLE);
        	 db.execSQL(EnterpriseTable.CREATE_TABLE);
            //创建索引
        	 db.execSQL(DictionaryTable.CREATE_INDEX);
        	 
        }

        @Override
        public synchronized void close() {
            super.close();
        }

        @Override
        public void onOpen(SQLiteDatabase db) {
            super.onOpen(db);
        }

        @Override
        /**
         * 数据库升级
         */
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        	if(oldVersion>1){
        		dropAllTables(db);
              	onCreate(db);
        	}
          	
        }

        /**
         * 删除用户信息
         * @param db
         */
        private void dropAllTables(SQLiteDatabase db) {
        	db.execSQL("DROP TABLE IF EXISTS " + UserTable.TABLE_NAME);
        	db.execSQL("DROP TABLE IF EXISTS " + OrgTable.TABLE_NAME);
        	db.execSQL("DROP TABLE IF EXISTS " + OrgRelationTable.TABLE_NAME);
        	db.execSQL("DROP TABLE IF EXISTS " + DictionaryTable.TABLE_NAME);
        	db.execSQL("DROP TABLE IF EXISTS " + UserInfoTable.TABLE_NAME);
        }
    }

    private UDatabase(Context context) {
        mContext = context;
        mOpenHelper = new DatabaseHelper(context);
    }

    public static synchronized UDatabase getInstance(Context context) {
        if (null == instance) {
            return new UDatabase(context);
        }
        return instance;
    }

    // 测试用
    public SQLiteOpenHelper getSQLiteOpenHelper() {
        return mOpenHelper;
    }
    
    public static SQLiteDatabase getDb(boolean writeable) {
        if (writeable) {
            return mOpenHelper.getWritableDatabase();
        } else {
            return mOpenHelper.getReadableDatabase();
        }
    }

    public void close() {
        if (null != instance) {
            mOpenHelper.close();
            instance = null;
        }
    }

    /**
     * 清空所有表中数据, 谨慎使用
     */
    public void clearData() {
    	 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    }

    /** 
     * 直接删除数据库文件, 调试用
     */
    private boolean deleteDatabase() {
        File dbFile = mContext.getDatabasePath(DATABASE_NAME);
        return dbFile.delete();
    }
}